package com.yishusmt.wechatapi.wechatdev.dto.base;

import com.yishusmt.wechatapi.wechatdev.enums.ResultCodeEnum;
import lombok.Data;
import org.apache.commons.lang.StringUtils;

/**
 * 统一返回json格式
 *
 * @author zhangyi
 * @date 2018/8/27 14:53
 */
@Data
public class Result<T> {
    /**
     * 结果码
     */
    private Integer status;

    /**
     * 返回数据
     */
    private T data;

    /**
     * 返回信息
     */
    private String msg;

    /**
     * 返回时间
     */
    private String respTime;

    /**
     * 成功时的构造器
     */
    public Result(T data) {
        this.data = data;
        this.status = ResultCodeEnum.OPERATION_SUCCESS.key();
        this.setMsg(ResultCodeEnum.OPERATION_SUCCESS.desc());
    }

    /**
     * 错误时的构造器
     */
    public Result(ResultCodeEnum resultCodeEnum, String errorMsg) {
        this.status = resultCodeEnum.key();
        if (StringUtils.isEmpty(errorMsg)) {
            this.msg = resultCodeEnum.desc();
        } else {
            this.msg = errorMsg;
        }
    }
    @Override
    public String toString() {
        return "Result{" + "status=" + status + ", data=" + data + ", msg='" + msg + '\'' + ", respTime='" + respTime + '\'' + '}';
    }
}
